package com.gitee.hljdrl.dialog.visitor;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.DrawableRes;

import com.gitee.hljdrl.dialog.R;
import com.gitee.hljdrl.dialog.UIDialog;
import com.gitee.hljdrl.dialog.util.UIUtil;


public class DownloadVisitor extends BasisViewPartVisitor {

    private View mPanel;
    private TextView mTvTitle;
    private TextView mProgressNum;
    private ProgressBar mProgressBar;
    //-------------------------------------------
    private int mBackground = 0;
    private CharSequence mProgressText;
    private boolean mShowCloseButton = true;

    public DownloadVisitor(Context context) {
        super(context);
        setLayout(R.layout.dialog_download_part);
        setDialogTheme(R.style.UIDownLoadDialog);
    }

    public DownloadVisitor setBackground(@DrawableRes int background) {
        mBackground = background;
        return this;
    }

    public DownloadVisitor setShowClose(boolean show) {
        this.mShowCloseButton = show;
        return this;
    }

    //----------------------------------------------------------
    public void setProgressMax(int max) {
        if (!isDismiss() && mProgressBar != null && max > 0) {
            mProgressBar.setMax(max);
        }
    }

    public void setProgress(int progress) {
        if (!isDismiss() && mProgressBar != null) {
            if (progress <= mProgressBar.getMax() && progress >= 0) {
                mProgressBar.setProgress(progress);
            }
        }
    }

    public void setProgressText(CharSequence progressText) {
        if (!isDismiss()) {
            mProgressText = progressText;
            if (mProgressNum != null) {
                mProgressNum.setText(progressText);
            }
        }
    }

    @Override
    protected boolean enablePromptVisit() {
        return false;
    }

    @Override
    public void onViewVisit(Dialog dialog, View view) {
        mPanel = view.findViewById(R.id.dialog_panel);
        mTvTitle = view.findViewById(R.id.tv_title);
        mProgressNum = view.findViewById(R.id.tv_progress_num);
        mProgressBar = view.findViewById(R.id.pb_download);
        ImageView ivClose = view.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(v -> UIUtil.dismissDialog(dialog));
        if (!mShowCloseButton) {
            ivClose.setVisibility(View.INVISIBLE);
        }
        if (mTvTitle != null) {
            mTvTitle.setText(mTitle);
        }
        if (mProgressText != null) {
            mProgressNum.setText(mProgressText);
        }
        if (mBackground != 0) {
            mPanel.setBackgroundResource(mBackground);
        }
        super.onViewVisit(dialog, view);
    }

    @Override
    protected Dialog createDialog(Context context, int theme) {
        UIDialog dialog = new UIDialog(context, theme);
        dialog.setViewPartVisitor(this);
        return dialog;
    }

}
